package com.zlp.api.controller;

import com.zlp.api.dao.GoodsRepository;
import com.zlp.api.entity.Googs;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：ZouLiPing
 * @description：Elasticsearch
 * @date ： 2019/9/2 16:27
 */
@RestController
public class ElasticsearchController {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private GoodsRepository coodsRepository;

    /**
     * 创建 Goods 文档对象
     *
     */
    @GetMapping("createIndex")
    public void createIndex() {
        elasticsearchTemplate.createIndex(Googs.class);
    }

    @GetMapping("insert")
    public void insert() {
        Googs item = new Googs(1L, "小米手机7", " 手机", "小米", 349900L, "http://image.baidu.com/13123.jpg");
        coodsRepository.save(item);
    }

    /**
     * @desc 定义批量新增方法
     *
     */
    @GetMapping("insertList")
    public void insertList() {
        List<Googs> list = new ArrayList<>();
        list.add(new Googs(4L, "小米R1", " 手机", "锤子", 369900L, "http://image.baidu.com/13123.jpg"));
        list.add(new Googs(5L, "vivo10", " 手机", "华为", 280000L, "http://image.baidu.com/13123.jpg"));
        list.add(new Googs(6L, "华为META10", " 手机", "华为", 44993L, "http://image.baidu.com/13123.jpg"));
        list.add(new Googs(7L, "华为META10", " 手机", "华为", 44993L, "http://image.baidu.com/13123.jpg"));
        list.add(new Googs(8L, "华为META10", " 手机", "华为", 44993L, "http://image.baidu.com/13123.jpg"));
        list.add(new Googs(9L, "华为META10", " 手机", "华为", 44993L, "http://image.baidu.com/13123.jpg"));
        // 接收对象集合，实现批量新增
        coodsRepository.saveAll(list);
    }

    /**
     * 定义修改方法
     */
    @GetMapping("update")
    public void update(){
        Googs item = new Googs(1L, "苹果XSMax", " 手机",
                "小米", 349923L, "http://image.baidu.com/13123.jpg");
        coodsRepository.save(item);
    }

    /**
     * 定义查询方法,含对价格的降序、升序查询
     */
    @GetMapping("testQueryAll")
    public void testQueryAll(){
        // 查找所有
        //Iterable<Item> list = this.itemRepository.findAll();
        // 对某字段排序查找所有 Sort.by("price").descending() 降序
        // Sort.by("price").ascending():升序
        Iterable<Googs> list = this.coodsRepository.findAll(Sort.by("price").ascending());

        for (Googs item:list){
            System.out.println(item);
        }
    }

    /**
     * @Description:按照价格区间查询
     */
    @GetMapping("queryByPriceBetween")
    public List<Googs> queryByPriceBetween(){
        List<Googs> list = this.coodsRepository.findByPriceBetween(200000L, 370000L);
        for (Googs item : list) {
            System.out.println("item = " + item.toString());
        }
        return list;
    }

    /**
     * @description:matchQuery底层采用的是词条匹配查询
     */
    @GetMapping("testMatchQuery")
    public Page<Googs> testMatchQuery(){
        // 构建查询条件
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        // 添加基本分词查询
        queryBuilder.withQuery(QueryBuilders.matchQuery("title", "小米手机"));
        // 搜索，获取结果
        Page<Googs> items = this.coodsRepository.search(queryBuilder.build());
        // 总条数
        long total = items.getTotalElements();
        System.out.println("total = " + total);
        for (Googs goods : items) {
            System.out.println(goods.toString());
        }
        return items;
    }






}
